package com.gdit.zax.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gdit.zax.dao.AttentionDao;
import com.gdit.zax.dao.PlanDao;
import com.gdit.zax.entitys.Plan;
import com.gdit.zax.factory.DAOFactory;
import com.gdit.zax.utils.DBUtil;
import com.gdit.zax.utils.LogUtil;
import com.gdit.zax.utils.O;
import com.gdit.zax.utils.TextUtil;
import com.gdit.zax.utils.UrlDecoder;
import com.gdit.zax.utils.VeriFicUtil;

public class AddPlan extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
//		request.setCharacterEncoding("ISO-8859-1");
//		response.setCharacterEncoding("ISO-8859-1");
		this.doPost(request, response);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String accesstoken = request.getParameter("accesstoken");
		String theme = request.getParameter("theme");
		
		int id = TextUtil.String2int(request.getParameter("id"), -1);
		int tipType  =TextUtil.String2int(request.getParameter("tiptype"), -1);
		String planTime = request.getParameter("plantime"); 
		long mplanTime = 0;
		
		//检测参数
		if (TextUtil.isEmpty(accesstoken)) {
			O.printFail(response, 1, "请检查accesstoken参数!");
			return;
		}
		if (TextUtil.isEmpty(theme)) {
			O.printFail(response, 1, "请检查theme参数!");
			return;
		}
		if (id<0) {
			O.printFail(response, 1, "请检查id参数!");
			return;
		}
		if (tipType<0) {
			O.printFail(response, 1, "请检查tipType参数!");
			return;
		}
		if (TextUtil.isEmpty(planTime)) {
			O.printFail(response, 1, "请检查planTime参数!");
			return;
		}
		try {
			mplanTime  = Long.parseLong(planTime);
		} catch (Exception e) {
		}
		if (mplanTime + 1000*60*60*12 < System.currentTimeMillis()) {
			O.printFail(response, 1, "计划的时间最小也要半天吧?!");
			return;
		}
		
		
		
		//检测连接、用户
		int uid = VeriFicUtil.getVFID(accesstoken);
		Connection conn = DBUtil.getConnection();
		
		if (uid==Integer.MIN_VALUE || uid==Integer.MAX_VALUE) {
			O.printNoLogin(response);
			return;
		}
		
		if (conn==null) {
			O.printFail(response, 1000, "数据库连接失败!");
			return;
		}
		
		
		DAOFactory dao  = DAOFactory.init(conn, request, response);
		PlanDao pdao = dao.getPlanDao();
		Plan plan = new Plan();
		plan.setContent(theme);
		plan.setUserid(uid);
		plan.setCouresid(id);
		plan.setType(tipType);
		plan.setPlantime(mplanTime);
		
		
		HashMap<String, Object> map = new HashMap<String, Object>();
		if (pdao.addPlan(plan)) {
			map.put("errorcode", 0);
			map.put("message", "添加成功!");
			
			plan = pdao.getMyLastPlanInfo(uid);
			map.put("planid", plan.getId());
		} else {
			map.put("errorcode", 1);
			map.put("message", "添加失败!");
		}
		O.print(response, map);
		
		
		
	}

}
